package com.jt.www.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.jt.www.common.service.FileService;
import com.jt.www.common.service.SendEmailsService;
import com.jt.www.domain.vo.MonitorErrorVO;
import com.jt.www.enums.endorse.PolicyTypeEnum;
import com.jt.www.model.mail.dto.MailBean;
import com.jt.www.model.mail.dto.MailDTO;
import com.jt.www.model.mail.vo.AccountVo;
import com.jt.www.model.mail.vo.JtBranchAccountVo;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.remote.ProductClient;
import com.jt.www.remote.UserClient;
import com.jt.www.util.DateUtils;
import com.jt.www.util.mail.MailUtil;
import com.jt.www.util.mail.WordAction;
import feign.Response;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 发送邮件
 * @author: xuxinku
 * @Date: 2019/6/16 14:01
 * @ModifiedDate：
 * @Copyright:江泰保险股份有限公司
 */
@Service
public class SendeEmailsServiceImpl implements SendEmailsService {

    public static final Logger log = LoggerFactory.getLogger(SendeEmailsServiceImpl.class);

    @Resource
    UserClient userClient;
    @Resource
    ProductClient productClient;
    // 注入模板引擎
    @Autowired
    TemplateEngine templateEngine;
    @Autowired
    MailUtil mailUtil;

    @Autowired
    FileService fileService;
    @Autowired
    JavaMailSender mailSender;

    /**
     * 从输入流中获取字节数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

    /**
     * 校验EMAIL格式，真为正确
     *
     * @param email
     * @return true 为格式正确 false 为格式错误
     * @author
     * @date 2019-7-16
     */
    public static boolean emailFormat(String email) {
        boolean tag = true;
        if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
            tag = false;
        }
        return tag;
    }

    /**
     * 根据状态来选择要执行的邮件类型
     *
     * @param mailDTO 参数信息
     * @param type    邮件类型状态
     */
    @Override
    public ResponseEntity sendEmail(MailDTO mailDTO, String type) {

        if ("".equals(mailDTO.getEmail()) || null == mailDTO.getEmail()) {
            log.info("============>收件人邮箱为空");
        } else {
            log.info("============>发送邮件初始化  =》开始请求！邮箱地址" + mailDTO.getEmail() + "前端入参" + JSON.toJSON(mailDTO));
        }

        try {

            boolean b = emailFormat(mailDTO.getEmail());
            if (!b) {
                log.info("============>收件人邮箱格式不正确！请核对邮箱" + mailDTO.getEmail());
            }
            log.info("发送邮件开始==========》收件人邮箱：" + mailDTO.getEmail() + "邮件类型状态" + type);
            if ("1".equals(type)) {
                ZCXXTJ(mailDTO);
                log.info("注册信息提交通知===结束===>" + "收件人邮箱：" + mailDTO.getEmail());
            } else if ("2".equals(type)) {
                ZCXXSHBTG(mailDTO);
                log.info("注册信息审核不通过通知 ====结束==>" + "收件人邮箱：" + mailDTO.getEmail());
            } else if ("3".equals(type)) {
                ZCSHTG(mailDTO);
                log.info("注册信息审核通过通知 ====结束==>" + "收件人邮箱：" + mailDTO.getEmail());
            } else if ("04".equals(type)) {
                //BDYTJ(mailDTO);
                //log.debug("投保单提交成功通知 投保单已提交状态======>");
            } else if ("4".equals(type)) {
                BDYTJ4(mailDTO);
                log.info("投保单提交成功通知（投保单已提交状态）====结束==>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("5".equals(type)) {
                FHBTG(mailDTO);
                log.info("已提交状态的投保单复核不通过通知 ===结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("06".equals(type)) {
                BDYFH(mailDTO);
                log.info("投保单提交成功通知（投保单已复核状态）统保、学生安责、云南非九===结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("6".equals(type)) {
                BDYFH6(mailDTO);
                log.info("已提交状态的投保单初审通过通知  统保、学生安责===结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("7".equals(type)) {
                //TBDZTDSH(mailDTO);
                //log.debug("单证审核未通过通知（投保单状态待审核）==结束===>");
            } else if ("8".equals(type)) {
                //TBDZTDSHTG(mailDTO);
                //log.debug("单证审核通过通知（投保单状态待审核）===结束==>");
            } else if ("9".equals(type)) {
                TBDSC(mailDTO);
                log.info("投保单删除通知=结束====>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("10".equals(type)) {
                //PDSQTZ(mailDTO);
                //log.debug("批改申请提交通知==结束===>");
            } else if ("11".equals(type)) {
                PDYYJSCHTZ(mailDTO);
                log.info("批改申请提交通知===结束==>" + "收件人邮箱：" + mailDTO.getEmail() + "批单申请号" + mailDTO.getCorrectNo());
            } else if ("12".equals(type)) {
                //PDYYJFHTGTZ(mailDTO);
                //log.debug("批单影印件复核通过通知===结束==>");
            } else if ("13".equals(type)) {
                PDBFZJ(mailDTO);
                log.info("批改申请初审通过通知-------保费增加-----统保、学生安责、云南==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "批单申请号" + mailDTO.getCorrectNo());
            } else if ("013".equals(type)) {
                PDBFZJ2(mailDTO);
                log.info("批改申请初审通过通知-------保费增加-----山东===结束==>" + "收件人邮箱：" + mailDTO.getEmail() + "批单申请号" + mailDTO.getCorrectNo());
            } else if ("14".equals(type)) {
                PDYYJWTG(mailDTO);
                log.info("批改申请初审通过通知-------保费无变化或减少==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "批单申请号" + mailDTO.getCorrectNo());
            } else if ("15".equals(type)) {
                DZSHTG(mailDTO);
                log.info("单证审核通过通知（投保单状态待复核）--------统保、学生安责、云南 ==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("015".equals(type)) {
                DZSHTG2(mailDTO);
                log.info("批改申请复审通过通知-----统保、学生安责、云南 ==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "批单申请号" + mailDTO.getCorrectNo());
            } else if ("16".equals(type)) {
                DZSHBTG(mailDTO);
                log.info("单证审核未通过通知（投保单状态待复核）---------统保、学生安责、云南 ===结束==>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("0016".equals(type)) {
                DZSHBTG1(mailDTO);
                log.info("批改申请初审未通过通知===结束==>" + "收件人邮箱：" + mailDTO.getEmail() + "批单申请号" + mailDTO.getCorrectNo());
            } else if ("016".equals(type)) {
                DZSHBTG2(mailDTO);
                log.info("批改申请复审不通过通知------统保、学生安责、云南 ===结束==>" + "收件人邮箱：" + mailDTO.getEmail() + "批单申请号" + mailDTO.getCorrectNo());
            } else if ("17".equals(type)) {
                CDTZ(mailDTO);
                log.info("批单出单成功邮件===结束==>" + "收件人邮箱：" + mailDTO.getEmail() + "批单申请号" + mailDTO.getCorrectNo());
            } else if ("18".equals(type)) {
                //YNFJ(mailDTO);
                //log.debug("云南非九 投保单提交成功及缴费提醒通知=====>");
            } else if ("19".equals(type)) {
                SDXZ(mailDTO);
                log.info("已提交状态的投保单初审通过通知------山东校责 ==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("20".equals(type)) {
                //SDXZCDCG(mailDTO);
                //log.debug("山东校责 出单成功通知邮件=结束====>");
            } else if ("21".equals(type)) {
                CDCG(mailDTO);
                log.info("出单成功通知邮件==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("021".equals(type)) {
                CDCG2(mailDTO);
                log.info("出单成功通知邮件-----云南==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("22".equals(type)) {
                SXXZ(mailDTO);
                log.info("保费支付通知通知------山西校责 ==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("23".equals(type)) {
                GDDZFP(mailDTO);
                log.info("电子发票通知------广东4款产品 ==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            } else if ("99".equals(type)) {
                WXBELEC(mailDTO);
                log.info("我学保电子保单通知------我学保 ==结束===>" + "收件人邮箱：" + mailDTO.getEmail() + "投保单号" + mailDTO.getPolicyNo().toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.info("邮件发送失败" + e.getMessage() + "收件人邮箱：" + mailDTO.getEmail(), e);
            return GenericResponse.ng("邮件发送失败" + e.getMessage() + "收件人邮箱：" + mailDTO.getEmail());
        }
        return GenericResponse.ok();

    }

    /**
     * 注册信息提交通知【1】
     *
     * @param mailDTO 前端参数信息
     */
    public void ZCXXTJ(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("01emailTemplate", context);
        mailBean.setSubject("注册信息提交通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);
    }

//    /**
//     * 投保单提交成功通知（投保单已提交状态）  【04】
//     * 全国职业院校学生实习责任保险项目
//     * @param mailDTO
//     */
//    public void BDYTJ(MailDTO mailDTO){
//
//
//        MailBean mailBean = new MailBean();
//        Context context = new Context();
//        context.setVariable("type", "全国职业院校学生实习责任保险项目");
//        List<String> policyNo = mailDTO.getPolicyNo();
//        StringBuilder policyNos = new StringBuilder();
//        for (String pol:policyNo) {
//            policyNos.append(pol+". ");
//        }
//        context.setVariable("orderNo", "订单号");
//        context.setVariable("policyNo", mailDTO.getOrderNo());
//        context.setVariable("userdate", DateUtils.getCurrentTime());
//        String content = templateEngine.process("04emailTemplate", context);
//        mailBean.setSubject("投保单提交成功通知（投保单已提交状态）");
//        mailBean.setText(content);
//        mailBean.setEmail(mailDTO.getEmail());
//        mailUtil.sendMailHtml(mailBean);
//    }

    /**
     * 注册信息审核不通过通知  【2】
     *
     * @param mailDTO 前端参数信息
     */
    public void ZCXXSHBTG(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("reason", mailDTO.getReason());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("02emailTemplate", context);
        mailBean.setSubject("注册信息审核不通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);
    }

    /**
     * 注册信息审核通过通知  【3】
     *
     * @param mailDTO 前端参数信息
     */
    public void ZCSHTG(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("03emailTemplate", context);
        mailBean.setSubject("注册信息审核通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);
    }

    /**
     * 投保单提交成功通知（投保单已提交状态）  【4】
     *
     * @param mailDTO
     */
    public void BDYTJ4(MailDTO mailDTO) {


        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        //context.setVariable("policyNo", mailDTO.getPolicyNo());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);

        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        //由于山东产品特殊化  需要特殊处理【产品】
        String content = null;
        //存在
        if (mailDTO.getProductName().indexOf("山东") != -1) {
            content = templateEngine.process("004emailTemplate", context);
            //不存在
        } else {
            content = templateEngine.process("04emailTemplate", context);
        }


        mailBean.setSubject("投保单提交成功通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);
    }

    /**
     * 已提交状态的投保单复核不通过通知  【5】
     *
     * @param mailDTO
     */
    public void FHBTG(MailDTO mailDTO) {

        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("Reason", mailDTO.getReason());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("05emailTemplate", context);
        mailBean.setSubject("已提交状态的投保单复核不通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);
    }

    /**
     * 单证审核未通过通知（投保单状态待审核）【7】
     * @param mailDTO
     */

//    public void TBDZTDSH(MailDTO mailDTO){
//
//
//        MailBean mailBean = new MailBean();
//        Context context = new Context();
//        //校验是  全国职业院校学生实习责任保险项目01  或者 学生安全责任保险02
//        if("01".equals(mailDTO.getInsuranceType())){
//            context.setVariable("orderNo", "订单号");
//            context.setVariable("policyNo", mailDTO.getOrderNo());
//
//        }else if("02".equals(mailDTO.getInsuranceType())){
//            context.setVariable("orderNo", "投保单号");
//            List<String> policyNo = mailDTO.getPolicyNo();
//            StringBuilder policyNos = new StringBuilder();
//            for (String pol:policyNo) {
//                policyNos.append(pol+". ");
//            }
//            context.setVariable("policyNo", policyNos);
//        }
//        context.setVariable("Reason", mailDTO.getReason());
//        context.setVariable("userdate", DateUtils.getCurrentTime());
//        String content = templateEngine.process("07emailTemplate", context);
//        mailBean.setSubject("单证审核未通过通知（投保单状态待审核）");
//        mailBean.setText(content);
//        mailBean.setEmail(mailDTO.getEmail());
//        mailUtil.sendMailHtml(mailBean);
//
//    }

    /**
     * 单证审核通过通知（投保单状态待审核）【8】
     * @param mailDTO
     */
//    public void TBDZTDSHTG(MailDTO mailDTO){
//
//
//        MailBean mailBean = new MailBean();
//        Context context = new Context();
//
//        //校验是  全国职业院校学生实习责任保险项目01  或者 学生安全责任保险02
//        if("01".equals(mailDTO.getInsuranceType())){
//            context.setVariable("orderNo", "订单号");
//            context.setVariable("policyNo", mailDTO.getOrderNo());
//
//        }else if("02".equals(mailDTO.getInsuranceType())){
//            context.setVariable("orderNo", "投保单号");
//            List<String> policyNo = mailDTO.getPolicyNo();
//            StringBuilder policyNos = new StringBuilder();
//            for (String pol:policyNo) {
//                policyNos.append(pol+". ");
//            }
//            context.setVariable("policyNo", policyNos);
//        }
//
//        List<String> policyNo = mailDTO.getPolicyNo();
//        StringBuilder policyNos = new StringBuilder();
//        for (String pol:policyNo) {
//            policyNos.append(pol+". ");
//        }
//        context.setVariable("policyNo", policyNos);
//        context.setVariable("userdate", DateUtils.getCurrentTime());
//        String content = templateEngine.process("08emailTemplate", context);
//        mailBean.setSubject("单证审核通过通知（投保单状态待审核）");
//        mailBean.setText(content);
//        mailBean.setEmail(mailDTO.getEmail());
//        mailUtil.sendMailHtml(mailBean);
//
//    }

    /**
     * 投保单提交成功通知（投保单已复核状态）【06】
     * 附件3：保费支付通知书
     *
     * @param mailDTO
     */
    public void BDYFH(MailDTO mailDTO) {

        MailBean mailBean = MailBean.getMailBean();
        Context context = new Context();
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        context.setVariable("user", mailDTO.getUserName());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("userdate", DateUtils.getCurrentTime());

        String content = templateEngine.process("006emailTemplate", context);
        mailBean.setSubject("投保单提交成功通知");
        mailBean.setText(content);
        //mailBean.setAttachmentFilename("保费支付通知书.png");
        //资源下载
//        Response downloadFile = filesClient.downloadFile(1663339833290129408L);
//        Response.Body body = downloadFile.body();
//        InputStream inputStream =null;
//        InputStreamSource iss =null;
//        try {
//            inputStream = body.asInputStream();
//            byte[] bytes = readInputStream(inputStream);
//            iss = new ByteArrayResource(bytes);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        mailBean.setFile(iss);
        //Word 模板拼接
        WordAction wordAction = new WordAction();
        //调用用户中心 根据省市区查询机构账户信息
        ResponseEntity<GenericDataResponse<JtBranchAccountVo>> eduZH = null;
        try {
            eduZH = userClient.branchAccount(mailDTO.getAreaCode(), "edu", mailDTO.getProductCode());
        } catch (Exception e) {
            log.info("发送邮件失败:调用用户中心 根据省市区查询机构账户信息===》邮箱：" + mailDTO.getEmail(), e);
            e.printStackTrace();
        }
        GenericDataResponse<JtBranchAccountVo> body = eduZH.getBody();
        if (null != body) {
            JtBranchAccountVo data = body.getData();
            List<AccountVo> accountVoList = data.getAccountVoList();
            for (AccountVo account : accountVoList) {
                //户名
                mailDTO.setAccount(account.getBankName());
                //开户行
                mailDTO.setBank(account.getBankAccountName());
                //账号
                mailDTO.setNumber(account.getBankAccountNo());
            }
        }
        String s = wordAction.wordTest("1", mailDTO);
        FileSystemResource file = new FileSystemResource(new File(s));
        mailBean.setAttachmentFilename2("保费支付通知书.doc");
        mailBean.setFile2(file);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailAttachment(mailBean);

        //删除附件
        try {
            java.io.File myDelFile = new java.io.File(s);
            myDelFile.delete();
            log.info("删除文件成功==========》" + s);

        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除文件失败" + s, e);

        }
    }

    /**
     * 批改申请提交通知【10】
     * @param mailDTO
     */

//    public void PDSQTZ(MailDTO mailDTO){
//
//        MailBean mailBean = new MailBean();
//        Context context = new Context();
//        context.setVariable("PDstartDate", mailDTO.getPdStartDate());
//        context.setVariable("user", mailDTO.getUserName());
//        context.setVariable("productName", mailDTO.getProductName());
//        context.setVariable("correctNo", mailDTO.getCorrectNo());
//        context.setVariable("userdate", DateUtils.getCurrentTime());
//        String content = templateEngine.process("10emailTemplate", context);
//        mailBean.setSubject("批改申请提交通知");
//        mailBean.setText(content);
//        mailBean.setEmail(mailDTO.getEmail());
//        mailUtil.sendMailHtml(mailBean);
//
//    }

    /**
     * 已提交状态的投保单初审通过通知【6】
     * 附件3：保费支付通知书
     *
     * @param mailDTO
     */
    public void BDYFH6(MailDTO mailDTO) {

        MailBean mailBean = MailBean.getMailBean();
        Context context = new Context();
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        context.setVariable("user", mailDTO.getUserName());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("06emailTemplate", context);
        mailBean.setSubject("已提交状态的投保单初审通过通知");
        mailBean.setText(content);
        //mailBean.setAttachmentFilename("保费支付通知书.png");
        //资源下载
//        Response downloadFile = filesClient.downloadFile(1663339833290129408L);
//        Response.Body body = downloadFile.body();
//        InputStream inputStream =null;
//        InputStreamSource iss =null;
//        try {
//            inputStream = body.asInputStream();
//            byte[] bytes = readInputStream(inputStream);
//            iss = new ByteArrayResource(bytes);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        mailBean.setFile(iss);
        //Word 模板拼接
        WordAction wordAction = new WordAction();
        //调用用户中心 根据省市区查询机构账户信息
        ResponseEntity<GenericDataResponse<JtBranchAccountVo>> eduZH = null;
        try {
            eduZH = userClient.branchAccount(mailDTO.getAreaCode(), "edu", mailDTO.getProductCode());
        } catch (Exception e) {
            log.info("发送邮件失败:调用用户中心 根据省市区查询机构账户信息===》邮箱：" + mailDTO.getEmail(), e);
            e.printStackTrace();
        }
        GenericDataResponse<JtBranchAccountVo> body = eduZH.getBody();
        if (null != body) {
            JtBranchAccountVo data = body.getData();
            List<AccountVo> accountVoList = data.getAccountVoList();
            for (AccountVo account : accountVoList) {
                //户名
                mailDTO.setAccount(account.getBankName());
                //开户行
                mailDTO.setBank(account.getBankAccountName());
                //账号
                mailDTO.setNumber(account.getBankAccountNo());
            }
        }
        String s = wordAction.wordTest("1", mailDTO);
        FileSystemResource file = new FileSystemResource(new File(s));
        mailBean.setAttachmentFilename2("保费支付通知书.doc");
        mailBean.setFile2(file);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailAttachment(mailBean);

        //删除附件
        try {
            java.io.File myDelFile = new java.io.File(s);
            myDelFile.delete();
            log.info("删除文件成功==========》" + s);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除文件失败" + s, e);

        }
    }


    /**
     * 批单影印件复核通过通知【12】
     * @param mailDTO
     */

//    public void PDYYJFHTGTZ(MailDTO mailDTO){
//        MailBean mailBean = new MailBean();
//        Context context = new Context();
//        context.setVariable("correctNo", mailDTO.getCorrectNo());
//        context.setVariable("reason", mailDTO.getReason());
//        context.setVariable("userdate", DateUtils.getCurrentTime());
//        String content = templateEngine.process("12emailTemplate", context);
//        mailBean.setSubject("批单影印件复核通过通知");
//        mailBean.setText(content);
//        mailBean.setEmail(mailDTO.getEmail());
//        mailUtil.sendMailHtml(mailBean);
//
//    }

    /**
     * 投保单删除通知【9】
     *
     * @param mailDTO
     */
    public void TBDSC(MailDTO mailDTO) {

        String type = "";

        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("09emailTemplate", context);
        mailBean.setSubject("投保单删除通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     * 批改申请提交通知【11】
     *
     * @param mailDTO
     */
    public void PDYYJSCHTZ(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("pdStartDate", mailDTO.getPdStartDate());
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("11emailTemplate", context);
        mailBean.setSubject("批改申请提交通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);


    }

    /**
     * 批改申请初审通过通知-------保费增加-----统保、学生安责、云南【13】
     *
     * @param mailDTO
     */
    public void PDBFZJ(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("pdStartDate", mailDTO.getPdStartDate());
        //调用用户中心 根据省市区查询机构账户信息
        ResponseEntity<GenericDataResponse<JtBranchAccountVo>> eduZH = null;
        try {
            eduZH = userClient.branchAccount(mailDTO.getAreaCode(), "edu", mailDTO.getProductCode());
        } catch (Exception e) {
            log.info("发送邮件失败:调用用户中心 根据省市区查询机构账户信息===》邮箱：" + mailDTO.getEmail(), e);
            e.printStackTrace();
        }
        GenericDataResponse<JtBranchAccountVo> body = eduZH.getBody();
        if (null != body) {
            JtBranchAccountVo data = body.getData();
            List<AccountVo> accountVoList = data.getAccountVoList();
            for (AccountVo account : accountVoList) {
                //户名
                mailDTO.setAccount(account.getBankName());
                //开户行
                mailDTO.setBank(account.getBankAccountName());
                //账号
                mailDTO.setNumber(account.getBankAccountNo());
            }
        }

        context.setVariable("account", mailDTO.getBank());
        context.setVariable("bank", mailDTO.getAccount());
        context.setVariable("number", mailDTO.getNumber());
        context.setVariable("premium", mailDTO.getPremium());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("13emailTemplate", context);
        mailBean.setSubject("批改申请初审通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     * 批改申请初审通过通知-------保费增加-----山东【013】
     *
     * @param mailDTO
     */
    public void PDBFZJ2(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("pdStartDate", mailDTO.getPdStartDate());
        //调用用户中心 根据省市区查询机构账户信息
        ResponseEntity<GenericDataResponse<JtBranchAccountVo>> eduZH = null;
        try {
            eduZH = userClient.branchAccount(mailDTO.getAreaCode(), "edu", mailDTO.getProductCode());
        } catch (Exception e) {
            log.info("发送邮件失败:调用用户中心 根据省市区查询机构账户信息===》邮箱：" + mailDTO.getEmail(), e);
            e.printStackTrace();
        }
        GenericDataResponse<JtBranchAccountVo> body = eduZH.getBody();
        if (null != body) {
            JtBranchAccountVo data = body.getData();
            if (null != data) {
                List<AccountVo> accountVoList = data.getAccountVoList();
                for (AccountVo account : accountVoList) {
                    //户名
                    mailDTO.setAccount(account.getBankName());
                    //开户行
                    mailDTO.setBank(account.getBankAccountName());
                    //账号
                    mailDTO.setNumber(account.getBankAccountNo());
                }
            }

        }

        context.setVariable("account", mailDTO.getBank());
        context.setVariable("bank", mailDTO.getAccount());
        context.setVariable("number", mailDTO.getNumber());
        context.setVariable("region", mailDTO.getReason());
        String email = sdEmail(mailDTO.getRegion());
        context.setVariable("sdRegionEmail", email);
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("013emailTemplate", context);
        mailBean.setSubject("批改申请初审通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     * 批改申请初审通过通知-------保费无变化或减少【14】
     *
     * @param mailDTO
     */
    public void PDYYJWTG(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("PDstartDate", mailDTO.getPdStartDate());
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("14emailTemplate", context);
        mailBean.setSubject("批改申请初审通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     * 单证审核通过通知（投保单状态待复核）--------统保、学生安责、云南【15】
     *
     * @param mailDTO
     */
    public void DZSHTG(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("15emailTemplate", context);
        mailBean.setSubject("单证审核通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     * 批改申请复审通过通知-----统保、学生安责、云南【015】
     *
     * @param mailDTO
     */
    public void DZSHTG2(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("015emailTemplate", context);
        mailBean.setSubject("批改申请复审通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     * 单证审核未通过通知（投保单状态待复核）---------统保、学生安责、云南【16】
     *
     * @param mailDTO
     */
    public void DZSHBTG(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("reason", mailDTO.getReason());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("16emailTemplate", context);
        mailBean.setSubject("单证审核未通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     * 批改申请初审未通过通知【0016】
     *
     * @param mailDTO
     */
    public void DZSHBTG1(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("PDstartDate", mailDTO.getPdStartDate());
        context.setVariable("reason", mailDTO.getReason());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("0016emailTemplate", context);
        mailBean.setSubject("批改申请初审未通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     *  云南非九 投保单提交成功及缴费提醒通知【18】
     * 附件1：投保单[废除]
     * 附件2：保费支付通知书
     *
     * @param mailDTO
     */
//    public void YNFJ(MailDTO mailDTO){
//
//        MailBean mailBean = MailBean.getMailBean();
//        Context context = new Context();
//        context.setVariable("userName", mailDTO.getUserName());
//        List<String> policyNo = mailDTO.getPolicyNo();
//        StringBuilder policyNos = new StringBuilder();
//        for (String pol:policyNo) {
//            policyNos.append(pol+". ");
//        }
//        context.setVariable("policyNo", policyNos);
//        context.setVariable("startDate", mailDTO.getStartDate());
//        context.setVariable("userdate", DateUtils.getCurrentTime());
//
//        String content = templateEngine.process("YNemailTemplate", context);
//        mailBean.setSubject("云南非九 投保单提交成功及缴费提醒通知");
//        mailBean.setText(content);
//
//        //Word 模板拼接
//        WordAction wordAction = new WordAction();
//        //调用用户中心 根据省市区查询机构账户信息
//        ResponseEntity<GenericDataResponse<JtBranchAccountVo>> eduZH = userClient.branchAccount(mailDTO.getAreaCode(), "edu", mailDTO.getProductCode());
//        GenericDataResponse<JtBranchAccountVo> body = eduZH.getBody();
//        if(null!=body){
//            JtBranchAccountVo data = body.getData();
//            List<AccountVo> accountVoList = data.getAccountVoList();
//            for (AccountVo account:accountVoList) {
//                //户名
//                mailDTO.setAccount(account.getBankName());
//                //开户行
//                mailDTO.setBank(account.getBankAccountName());
//                //账号
//                mailDTO.setNumber(account.getBankAccountNo());
//            }
//        }
//        String s = wordAction.wordTest("2",mailDTO);
//        FileSystemResource file = new FileSystemResource(new File(s));
//
//        mailBean.setAttachmentFilename2("保费支付通知书.doc");
//        mailBean.setFile2(file);
//        mailBean.setEmail(mailDTO.getEmail());
//        mailUtil.sendMailAttachment(mailBean);
//
//        //删除附件
//        try {
//            java.io.File myDelFile = new java.io.File(s);
//            myDelFile.delete();
//            log.debug("删除文件成功==========》"+s);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new BizException("删除文件失败");
//        }
//
//    }

    /**
     * 批改申请复审不通过通知------统保、学生安责、云南【016】
     *
     * @param mailDTO
     */
    public void DZSHBTG2(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("reason", mailDTO.getReason());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("016emailTemplate", context);
        mailBean.setSubject("批改申请复审不通过通知");
        mailBean.setText(content);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     *  山东校责 出单成功通知邮件【20】
     *
     * @param mailDTO
     */
//    public void SDXZCDCG(MailDTO mailDTO){
//
//        MailBean mailBean = new MailBean();
//        Context context = new Context();
//        context.setVariable("userName", mailDTO.getUserName());
//        List<String> policyNo = mailDTO.getPolicyNo();
//        StringBuilder policyNos = new StringBuilder();
//        for (String pol:policyNo) {
//            policyNos.append(pol+". ");
//        }
//        context.setVariable("policyNo", policyNos);
//        context.setVariable("startDate", mailDTO.getStartDate());
//        context.setVariable("endDate", mailDTO.getEndDate());
//        context.setVariable("proposalNo", mailDTO.getProposalNo());
//        context.setVariable("userdate", DateUtils.getCurrentTime());
//        String content = templateEngine.process("SD02emailTemplate", context);
//        mailBean.setSubject("山东校责 出单成功通知邮件");
//        mailBean.setText(content);
//        mailBean.setEmail(mailDTO.getEmail());
//        mailUtil.sendMailHtml(mailBean);
//
//
//
//    }

    /**
     * 批单出单成功邮件【17】--
     *
     * @param mailDTO
     */
    public void CDTZ(MailDTO mailDTO) {
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("correctNo", mailDTO.getCorrectNo());
        context.setVariable("PDstartDate", mailDTO.getPdStartDate());
        context.setVariable("batchNo", mailDTO.getBatchNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("17emailTemplate", context);
        mailBean.setSubject("批单出单成功邮件");
        mailBean.setText(content);

        //附件2  资源下载
//        Response downloadFile = filesClient.downloadFile(1663339833290129408L);
//        Response.Body body = downloadFile.body();
//        InputStream inputStream =null;
//        InputStreamSource iss =null;
//        try {
//            inputStream = body.asInputStream();
//            byte[] bytes = readInputStream(inputStream);
//            iss = new ByteArrayResource(bytes);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        mailBean.setFile(iss);
//        mailBean.setAttachmentFilename("测试附件.png");


        mailBean.setEmail(mailDTO.getEmail());
        //mailUtil.sendMailAttachment(mailBean);
        mailUtil.sendMailHtml(mailBean);

    }

    /**
     * 已提交状态的投保单初审通过通知【19】
     * 附件3：保费支付通知书
     *
     * @param mailDTO
     */
    public void SDXZ(MailDTO mailDTO) {
        MailBean mailBean = MailBean.getMailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("orderNo", mailDTO.getOrderNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        context.setVariable("productName", mailDTO.getProductName());
        String content = templateEngine.process("SD01emailTemplate", context);
        if (mailDTO.getProductCode().equals(PolicyTypeEnum.GD_JZG.getProductCode()) || mailDTO.getProductCode().equals(PolicyTypeEnum.GD_SP.getProductCode())
                || mailDTO.getProductCode().equals(PolicyTypeEnum.GD_XC.getProductCode()) || mailDTO.getProductCode().equals(PolicyTypeEnum.GD_XZ.getProductCode())) {
            mailBean.setSubject("投保单提交成功通知");
        } else {
            mailBean.setSubject("已提交状态的投保单初审通过通知");
        }
        mailBean.setText(content);
        //Word 模板拼接
        WordAction wordAction = new WordAction();
        //调用用户中心 根据省市区查询机构账户信息
        ResponseEntity<GenericDataResponse<JtBranchAccountVo>> eduZH = null;
        try {
            eduZH = userClient.branchAccount(mailDTO.getAreaCode(), "edu", mailDTO.getProductCode());
        } catch (Exception e) {
            log.info("发送邮件失败:调用用户中心 根据省市区查询机构账户信息===》邮箱：" + mailDTO.getEmail(), e);
            e.printStackTrace();
        }
        GenericDataResponse<JtBranchAccountVo> body = eduZH.getBody();
        if (null != body) {
            JtBranchAccountVo data = body.getData();
            List<AccountVo> accountVoList = data.getAccountVoList();
            for (AccountVo account : accountVoList) {
                //户名
                mailDTO.setAccount(account.getBankName());
                //开户行
                mailDTO.setBank(account.getBankAccountName());
                //账号
                mailDTO.setNumber(account.getBankAccountNo());
            }
        }
        String s = null;
        if (mailDTO.getProductCode().equals(PolicyTypeEnum.GD_JZG.getProductCode()) || mailDTO.getProductCode().equals(PolicyTypeEnum.GD_SP.getProductCode())
                || mailDTO.getProductCode().equals(PolicyTypeEnum.GD_XC.getProductCode()) || mailDTO.getProductCode().equals(PolicyTypeEnum.GD_XZ.getProductCode())) {
            s = wordAction.wordTest("5", mailDTO);
        } else {
            s = wordAction.wordTest("3", mailDTO);
        }

        FileSystemResource file = new FileSystemResource(new File(s));

        mailBean.setAttachmentFilename2("保费支付通知书.doc");
        mailBean.setFile2(file);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailAttachment(mailBean);

        //删除附件
        try {
            java.io.File myDelFile = new java.io.File(s);
            myDelFile.delete();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除文件失败" + s, e);
        }

    }

    /**
     * 出单成功通知邮件【21】
     *
     * @param mailDTO
     */
    public void CDCG(MailDTO mailDTO) {

        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("proposalNo", mailDTO.getProposalNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("21emailTemplate", context);
        mailBean.setSubject("出单成功通知");
        mailBean.setText(content);

        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailHtml(mailBean);


    }

    /**
     * 出单成功通知邮件-----云南【021】
     * <p>
     * 云南
     *
     * @param mailDTO
     */
    public void CDCG2(MailDTO mailDTO) {
        String findCode = "CDCG2_" + System.currentTimeMillis();

        log.info("{} 发送电子保单邮件 入参 {}", findCode, mailDTO);

        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("orderNo", mailDTO.getOrderNo());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("proposalNo", mailDTO.getProposalNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("21emailTemplate", context);
        mailBean.setSubject("出单成功通知邮件");
        mailBean.setText(content);
        //附件2  资源下载
        if (!StringUtils.isEmpty(mailDTO.getPolicyId())) {
            Response downloadFile = null;
            try {
                downloadFile = fileService.downloadFileByFileUrl(mailDTO.getPolicyId());

                log.info("{} 发送电子保单邮件 downloadFile {}", findCode, downloadFile);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.info("发送邮件失败:资源下载===》邮箱：" + mailDTO.getEmail(), e);
            }
            Response.Body body = downloadFile.body();
            InputStream inputStream = null;
            InputStreamSource iss = null;
            try {
                inputStream = body.asInputStream();
                byte[] bytes = readInputStream(inputStream);
                log.info("{} 发送电子保单邮件 bytes.length {}", findCode, bytes.length);
                iss = new ByteArrayResource(bytes);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("发送邮件失败:从输入流中获取字节数组===》邮箱：" + mailDTO.getEmail(), e);
            }
            mailBean.setFile(iss);
            mailBean.setAttachmentFilename("电子保单.pdf");

            mailBean.setEmail(mailDTO.getEmail());
            mailUtil.sendMailAttachment(mailBean);
        } else {
            mailBean.setEmail(mailDTO.getEmail());
            mailUtil.sendMailHtml(mailBean);
        }


    }

    /**
     * 山西校责已提交状态的投保单初审通过通知【22】
     * 附件3：保费支付通知书
     *
     * @param mailDTO
     */
    public void SXXZ(MailDTO mailDTO) {
        MailBean mailBean = MailBean.getMailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("policyNo", policyNos);
        context.setVariable("orderNo", mailDTO.getOrderNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
//        String email = sdEmail(mailDTO.getRegion());
//        context.setVariable("sdRegionEmail", email);
        String content = templateEngine.process("22emailTemplate", context);
        mailBean.setSubject("已提交状态的投保单初审通过通知");
        mailBean.setText(content);

        //Word 模板拼接
        WordAction wordAction = new WordAction();
        //调用用户中心 根据省市区查询机构账户信息
        ResponseEntity<GenericDataResponse<JtBranchAccountVo>> eduZH = null;
        try {
            eduZH = userClient.branchAccount(mailDTO.getAreaCode(), "edu", mailDTO.getProductCode());
        } catch (Exception e) {
            log.info("发送邮件失败:调用用户中心 根据省市区查询机构账户信息===》邮箱：" + mailDTO.getEmail(), e);
            e.printStackTrace();
        }
        GenericDataResponse<JtBranchAccountVo> body = eduZH.getBody();
        if (null != body) {
            JtBranchAccountVo data = body.getData();
            List<AccountVo> accountVoList = data.getAccountVoList();
            for (AccountVo account : accountVoList) {
                //户名
                mailDTO.setAccount(account.getBankName());
                //开户行
                mailDTO.setBank(account.getBankAccountName());
                //账号
                mailDTO.setNumber(account.getBankAccountNo());
            }
        }
        String s = wordAction.wordTest("4", mailDTO);
        FileSystemResource file = new FileSystemResource(new File(s));

        mailBean.setAttachmentFilename2("保费支付通知书.doc");
        mailBean.setFile2(file);
        mailBean.setEmail(mailDTO.getEmail());
        mailUtil.sendMailAttachment(mailBean);

        //删除附件
        try {
            java.io.File myDelFile = new java.io.File(s);
            myDelFile.delete();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除文件失败" + s, e);
        }

    }

    /**
     * 电子发票通知邮件-----广东【23】
     * <p>
     * 广东
     *
     * @param mailDTO
     */
    public void GDDZFP(MailDTO mailDTO) {
        String findCode = "GDDZFP_" + System.currentTimeMillis();

        log.info("{} 发送电子发票邮件 入参 {}", findCode, mailDTO);

        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;

        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("user", mailDTO.getUserName());
        context.setVariable("productName", mailDTO.getProductName());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol + ". ");
        }
        context.setVariable("orderNo", mailDTO.getOrderNo());
        context.setVariable("appNo", policyNos);
        context.setVariable("policyNo", mailDTO.getProposalNo());
        context.setVariable("userdate", DateUtils.getCurrentTime());
        String content = templateEngine.process("23emailTemplate", context);
        mailBean.setSubject("江泰电子发票" + month + "月" + day + "日【请勿直接回复】");
        mailBean.setText(content);
        //附件2  资源下载
        if (!StringUtils.isEmpty(mailDTO.getPolicyId())) {
            Response downloadFile = null;
            try {
                downloadFile = fileService.downloadFileByFileUrl(mailDTO.getPolicyId());

                log.info("{} 发送电子发票邮件 downloadFile {}", findCode, downloadFile);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.info("{} 发送电子发票邮件失败:资源下载===》邮箱：{}, error: {}", findCode, mailDTO.getEmail(), e);
            }
            Response.Body body = downloadFile.body();
            InputStream inputStream = null;
            InputStreamSource iss = null;
            try {
                inputStream = body.asInputStream();
                byte[] bytes = readInputStream(inputStream);
                log.info("{} 发送电子发票邮件 bytes.length {}", findCode, bytes.length);
                iss = new ByteArrayResource(bytes);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("{} 发送电子发票邮件失败:从输入流中获取字节数组===》邮箱：{}, error: {}", findCode, mailDTO.getEmail(), e);
            }
            mailBean.setFile(iss);
            mailBean.setAttachmentFilename("电子发票.pdf");

            mailBean.setEmail(mailDTO.getEmail());
            mailUtil.sendMailAttachment(mailBean);
        } else {
            mailBean.setEmail(mailDTO.getEmail());
            mailUtil.sendMailHtml(mailBean);
        }


    }

    /**
     * 我学保发送电子保单
     *
     * @param mailDTO
     */
    public void WXBELEC(MailDTO mailDTO) {

        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;

        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("productName", mailDTO.getProductName());
        context.setVariable("startDate", mailDTO.getStartDate());
        context.setVariable("endDate", mailDTO.getEndDate());
        List<String> policyNo = mailDTO.getPolicyNo();
        StringBuilder policyNos = new StringBuilder();
        for (String pol : policyNo) {
            policyNos.append(pol);
        }
        context.setVariable("policyNo", policyNos);
        String content = templateEngine.process("99emailTemplate", context);
        mailBean.setSubject("江泰电子保单" + month + "月" + day + "日【请勿直接回复】");
        mailBean.setText(content);
        //附件2  资源下载
        if (!StringUtils.isEmpty(mailDTO.getPolicyId())) {
            Response downloadFile = null;
            try {
                downloadFile = fileService.downloadFileByFileUrl(mailDTO.getPolicyId());
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.info("发送邮件失败:资源下载===》邮箱：" + mailDTO.getEmail(), e);
            }
            Response.Body body = downloadFile.body();
            InputStream inputStream = null;
            InputStreamSource iss = null;
            try {
                inputStream = body.asInputStream();
                byte[] bytes = readInputStream(inputStream);
                iss = new ByteArrayResource(bytes);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("发送邮件失败:从输入流中获取字节数组===》邮箱：" + mailDTO.getEmail(), e);
            }
            mailBean.setFile(iss);
            mailBean.setAttachmentFilename(policyNos + ".pdf");

            mailBean.setEmail(mailDTO.getEmail());
            mailUtil.sendMailAttachment(mailBean);
        } else {
            mailBean.setEmail(mailDTO.getEmail());
            mailUtil.sendMailHtml(mailBean);
        }


    }

    /**
     * 通过山东  和  山西 各市取出邮箱地址
     *
     * @return
     */
    public String sdEmail(String region) {
        String email = "";

        HashMap<String, String> map = new HashMap<>();
        //山东
        map.put("济南市", "jinan.yyb@jiangtai.com");
        map.put("潍坊市", "weifang.yyb@jiangtai.com");
        map.put("聊城市", "liaocheng.yyb@jiangtai.com");
        map.put("济宁市", "jining.yyb@jiangtai.com");
        map.put("烟台市", "yantai.yyb@jiangtai.com");
        map.put("威海市", "weihai.yyb@jiangtai.com");
        map.put("淄博市", "zibo.yyb@jiangtai.com");
        map.put("滨州市", "binzhou.yyb@jiangtai.com");
        map.put("德州市", "dezhou.yyb@jiangtai.com");
        map.put("莱芜市", "laiwu.yyb@jiangtai.com");
        map.put("临沂市", "linyi.yyb@jiangtai.com");
        map.put("日照市", "rizhao.yyb@jiangtai.com");
        map.put("东营市", "dongying.yyb@jiangtai.com");
        map.put("枣庄市", "zaozhuang.yyb@jiangtai.com");
        map.put("菏泽市", "heze.yyb@jiangtai.com");
        map.put("泰安市", "taian.yyb@jiangtai.com");
        //山西
        map.put("太原市", "taiyuan.xzx@jiangtai.com");
        map.put("大同市", "datong.xzx@jiangtai.com");
        map.put("朔州市", "shuozhou.xzx@jiangtai.com");
        map.put("忻州市", "xinzhou.xzx@jiangtai.com");
        map.put("吕梁市", "lvliang.xzx@jiangtai.com");
        map.put("阳泉市", "yangquan.xzx@jiangtai.com");
        map.put("长治市", "changzhi.xzx@jiangtai.com");
        map.put("晋城市", "jincheng.xzx@jiangtai.com");
        map.put("晋中市", "jinzhong.xzx@jiangtai.com");
        map.put("临汾市", "linfen.xzx@jiangtai.com");
        map.put("运城市", "yuncheng.xzx@jiangtai.com");


        //遍历map
        for (String key : map.keySet()) {
            //System.out.println("K== " + key + "V== " + map.get(key));

            //校验传来的key是否存在map中
            if (region.equals(key)) {
                email = map.get(region);
                return email;
            }
        }
        return email;

    }

    /**
     * 异常监控
     */
    public void monitorError(List<MonitorErrorVO> issueErrorList, List<MonitorErrorVO> ePolicyError) {
        if (CollectionUtils.isEmpty(issueErrorList) && CollectionUtils.isEmpty(ePolicyError)) {
            log.info("异常监控,无异常无需邮件通知!");
            return;
        }
        log.info("异常监控邮件通知开始!");
        MailBean mailBean = new MailBean();
        Context context = new Context();
        context.setVariable("userdate", DateUtils.getCurrentTime());
        context.setVariable("issueErrorList", issueErrorList);
        context.setVariable("ePolicyError", ePolicyError);

        String content = "出单异常：<br>" + issueErrorList.toString() + "<br><br>电子保单，电子发票异常：<br>" + ePolicyError.toString();
        mailBean.setSubject("异常监控");
        mailBean.setText(content);
        mailBean.setEmail("shaoyunan@jiangtai.com;wuxiaoqiang@jiangtai.com;guanhongxing@jiangtai.com;764669625@qq.com");
        mailUtil.sendMailHtml(mailBean);
        log.info("异常监控邮件通知结束!");
    }

}
